package com.mrper.ext.uibase;

import android.app.Activity;
import android.content.Context;
import android.databinding.DataBindingUtil;
import android.databinding.ViewDataBinding;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.KeyEvent;

import com.miguo.activity.base.BaseActivity;
import com.miguo.framework.request.FailData;
import com.miguo.framework.request.JsonData;
import com.miguo.http.callback.AjaxCallBack;
import com.miguo.ui.R;
import com.mrper.ext.annotation.BackActionEvent;
import com.mrper.util.ApkUtil;
import com.mrper.util.CommonUtil;
import com.nostra13.universalimageloader.core.ImageLoader;

/**
 * Created by admin on 2016/3/14.
 * Base Activity
 */
public class BasicActivity<B extends ViewDataBinding> extends BaseActivity implements OnInitContextInterface {

    /**
     * 数据绑定对象
     **/
    protected B dataBinder;
    /**
     * 上下文对象
     **/
    protected Context context;
    /**
     * activity对象
     **/
    protected Activity activity;
    /**
     * 网络回调对象
     **/
    protected AjaxCallBack httpCallback;
    //图片加载对象
    protected ImageLoader imageLoader = ImageLoader.getInstance();
    /**
     * 状态栏高度
     **/
    protected int statusBarHeight = 0;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        dataBinder = DataBindingUtil.setContentView(this, CommonUtil.inflateContentView(this));
        this.context = this; //获取context上下文对象
        this.activity = this; //设置activty获取对象
        statusBarHeight = ApkUtil.getStatusBarHeight(this); //获取状态栏高度
        httpCallback = isInitHttpCallback() ? getAjaxCallBack() : null;
    }

    @NonNull
    protected AjaxCallBack getAjaxCallBack() {
        return new AjaxCallBack(activity, getLoadingDialogString(), isInitShowLoadingDialog()) {
            @Override
            public void onRequestFinish(JsonData data) {
                super.onRequestFinish(data);
                onLoadHttpDataSuccessful(data);
                this.hideDialog();
            }

            @Override
            public void onRequestFail(FailData data) {
                super.onRequestFail(data);
                onLoadHttpDataFaield(data);
                this.hideDialog();
            }
        };
    }

    /**
     * 是否初始化HttpCallback
     **/
    public boolean isInitHttpCallback() {
        return true;
    }

    /**
     * 加载网络数据成功
     **/
    @Override
    public void onLoadHttpDataSuccessful(JsonData data) {
    }

    /**
     * 加载=网络数据失败
     **/
    @Override
    public void onLoadHttpDataFaield(FailData data) {
    }

    /**
     * 初始化加载对话框的字符串
     **/
    @Override
    public String getLoadingDialogString() {
        return getString(R.string.common_loading_message);
    }

    /**
     * 是否初始化时显示加载对话框,默认不显示
     **/
    @Override
    public boolean isInitShowLoadingDialog() {
        return false;
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (event.getAction() == KeyEvent.ACTION_DOWN && keyCode == KeyEvent.KEYCODE_BACK) {
            BackActionEvent ev = getClass().getAnnotation(BackActionEvent.class);
            if (ev != null && ev.isAllow()) {
                this.finish();
                overridePendingTransition(ev.enterAnimation(), ev.exitAnimation());
                return true;
            }
        }
        return super.onKeyDown(keyCode, event);
    }

}
